package com.ruoyi.jl_base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.jl_base.domain.bo.JlBaseBaWbsBo;
import com.ruoyi.jl_base.domain.vo.JlBaseBaWbsVo;
import com.ruoyi.jl_base.domain.JlBaseBaWbs;
import com.ruoyi.jl_base.mapper.JlBaseBaWbsMapper;
import com.ruoyi.jl_base.service.IJlBaseBaWbsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * wbs清单Service业务层处理
 *
 * @author zyb
 * @date 2024-12-23
 */
@RequiredArgsConstructor
@Service
public class JlBaseBaWbsServiceImpl implements IJlBaseBaWbsService {

    private final JlBaseBaWbsMapper baseMapper;

    /**
     * 查询wbs清单
     */
    @Override
    public JlBaseBaWbsVo queryById(Long wbsId){
        return baseMapper.selectVoById(wbsId);
    }

    /**
     * 查询wbs清单列表
     */
    @Override
    public TableDataInfo<JlBaseBaWbsVo> queryPageList(JlBaseBaWbsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JlBaseBaWbs> lqw = buildQueryWrapper(bo);
        Page<JlBaseBaWbsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询wbs清单列表
     */
    @Override
    public List<JlBaseBaWbsVo> queryList(JlBaseBaWbsBo bo) {
        LambdaQueryWrapper<JlBaseBaWbs> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<JlBaseBaWbs> buildQueryWrapper(JlBaseBaWbsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<JlBaseBaWbs> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getParentWbsId() != null, JlBaseBaWbs::getParentWbsId, bo.getParentWbsId());
        lqw.eq(bo.getContractId() != null, JlBaseBaWbs::getContractId, bo.getContractId());
        lqw.eq(StringUtils.isNotBlank(bo.getWbsBh()), JlBaseBaWbs::getWbsBh, bo.getWbsBh());
        lqw.eq(StringUtils.isNotBlank(bo.getWbsMc()), JlBaseBaWbs::getWbsMc, bo.getWbsMc());
        return lqw;
    }

    /**
     * 新增wbs清单
     */
    @Override
    public Boolean insertByBo(JlBaseBaWbsBo bo) {
        JlBaseBaWbs add = BeanUtil.toBean(bo, JlBaseBaWbs.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setWbsId(add.getWbsId());
        }
        return flag;
    }

    /**
     * 修改wbs清单
     */
    @Override
    public Boolean updateByBo(JlBaseBaWbsBo bo) {
        JlBaseBaWbs update = BeanUtil.toBean(bo, JlBaseBaWbs.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(JlBaseBaWbs entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除wbs清单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
